package prj.carmap.mobile.activity;

import java.util.ArrayList;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.util.ImgUtil;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class TestanroidpicActivity extends SherlockActivity implements OnPageChangeListener{

	ViewPager pager;
	ArrayList<String> urls ;
	private int position;
	private TextView pageText;
	private int imageCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		CarmapApplication.addActivity(this);
		setContentView(R.layout.activity_testanroidpic);
		pager = (ViewPager) findViewById(R.id.pager);
		
		pageText = (TextView) findViewById(R.id.page_text);
		
		View customNav = LayoutInflater.from(this).inflate(R.layout.nav_seller_title_view, null);
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);

		
		((TextView) customNav.findViewById(R.id.nav_title)).setText( "图片详情" );
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
		getSupportActionBar().setCustomView(customNav);
		customNav.setLayoutParams(lp);
		Button returnBtn = (Button) customNav.findViewById(R.id.return_prev);
		returnBtn.setOnClickListener(new Button.OnClickListener() {// 创建监听
					public void onClick(View v) {
						TestanroidpicActivity.this.finish();
					}
				});
		Button mapBtn = (Button) customNav.findViewById(R.id.return_map);
		mapBtn.setVisibility(View.GONE);
		urls = getIntent().getStringArrayListExtra("urls");
		position = Integer.parseInt(getIntent().getStringExtra("position"));
		pager = (ViewPager) findViewById(R.id.pager);
		
		String image[] = new String[urls.size()];
		imageCount = urls.size();
		pageText.setText((position + 1) + "/" + imageCount);
		for(int i=0;i<urls.size();i++){
			image[i]=CarmapApplication.SERVER_URL + "/images/" + urls.get(i);
		}
		pager.setOnPageChangeListener(this);
		pager.setAdapter(new ImagePagerAdapter(image));
		pager.setCurrentItem(position);

	}
	
	
	private class ImagePagerAdapter extends PagerAdapter {

		private String[] images;
		private LayoutInflater inflater;

		ImagePagerAdapter(String[] images) {
			this.images = images;
			inflater = getLayoutInflater();
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public int getCount() {
			return images.length;
		}

		@Override
		public Object instantiateItem(ViewGroup view, int position) {
			View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
			assert imageLayout != null;
//			ZoomImageView imageView = (ZoomImageView) imageLayout.findViewById(R.id.image);
			ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
			final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
			ImgUtil.loadImage(imageView, images[position],new SimpleImageLoadingListener(){
				@Override
				public void onLoadingStarted(String imageUri, View view) {
					spinner.setVisibility(View.VISIBLE);
				}
				@Override
				public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
					String message = null;
					switch (failReason.getType()) {
						case IO_ERROR:
							message = "Input/Output error";
							break;
						case DECODING_ERROR:
							message = "Image can't be decoded";
							break;
						case NETWORK_DENIED:
							message = "Downloads are denied";
							break;
						case OUT_OF_MEMORY:
							message = "Out Of Memory error";
							break;
						case UNKNOWN:
							message = "Unknown error";
							break;
					}
					Toast.makeText(TestanroidpicActivity.this, message, Toast.LENGTH_LONG).show();
					spinner.setVisibility(View.GONE);
				}
				
				@Override
				public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
					spinner.setVisibility(View.GONE);
				}
			},TestanroidpicActivity.this);
			view.addView(imageLayout, 0);
			return imageLayout;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view.equals(object);
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}
	}


	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onPageSelected(int currentPage) {
		pageText.setText((currentPage + 1) + "/" + imageCount);
		
	}


}
